package com.ftg.learn.service.impl;

import com.ftg.learn.common.dto.LoginDto;
import com.ftg.learn.common.util.JwtUtil;
import com.ftg.learn.common.util.KeyPairUtil;
import com.ftg.learn.common.util.R;
import com.ftg.learn.common.util.SecretBeanUtil;
import com.ftg.learn.common.vo.LoginVo;
import com.ftg.learn.remote.AuthOpenFeignRemote;
import com.ftg.learn.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 布衣
 */
@Service
@Primary
public class UserServiceImpl implements AuthService {

    @Autowired
    AuthOpenFeignRemote authRemote;

    @Autowired
    SecretBeanUtil secretBeanUtil;

    @Override
    public R<String> loginAndToken(LoginDto loginDto) {
        R<LoginVo> userInfo = authRemote.login(loginDto);
        if (userInfo != null && userInfo.getCode().equals(200)) {
            LoginVo data = userInfo.getData();
            Map<String,Object> map = new HashMap<>();
            map.put("username", data.getUsername());
            map.put("realname", data.getRealName());
            map.put("email", data.getEmail());
            map.put("phone", data.getPhone());
            map.put("dept_id", data.getDeptId());
            String token = JwtUtil.generateJWT(KeyPairUtil.getPrivateKey(secretBeanUtil.getPrivateKey()), map);
            return R.build(200, "登陆成功", token);
        }
        return R.build(500, "登陆失败", null);
    }

}
